package com.example.a1.fannong.core.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class BaseFragment extends Fragment {
    protected Logger logger = new Logger(getClass());

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().injectManager(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setText(getClass().getSimpleName());
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof BaseActivity)) {
            throw new RuntimeException("工程中所有的Activity必须继承BaseActivity");
        }
        super.onAttach(activity);
    }

    public Context getContext() {
        return getActivity();
    }

    /**
     * 获得管理器
     *
     * @param manager 管理器类型
     * @param <M>     管理器Class
     * @return 管理器
     */
    public <M extends BaseManager> M getManager(Class<M> manager) {
        return MyApplication.getInstance().getManager(manager);
    }

    public void showToast(int resId) {
        if (getActivity() == null) {
            return;
        }
        Toast.makeText(getActivity(), resId, Toast.LENGTH_SHORT).show();
    }

    public void showToast(String msg) {
        if (getActivity() == null || msg.length() == 0) {
            return;
        }
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }
}
